package top.hundreds.oasys.controller.FrontController;

import org.springframework.web.bind.annotation.*;
import top.hundreds.common.web.api.ApiResult;
import top.hundreds.oasys.bean.entity.Menu;
import top.hundreds.oasys.bean.vo.MenuTreeVo;
import top.hundreds.oasys.bean.vo.MenuVo;
import top.hundreds.oasys.service.MenuService;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Menu)表控制层
 *
 * @author lcf
 * @since 2020-12-02 19:29:32
 */
@RestController
@RequestMapping("/front/menu")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;

    @GetMapping("{id}")
    public ApiResult<Menu> get(@PathVariable Long id) {
        return ApiResult.success(this.menuService.get(id));
    }

    @GetMapping("/tree")
    public ApiResult<List<MenuTreeVo>> tree() {
        return ApiResult.success(this.menuService.getTree());
    }

    @GetMapping("/list")
    public ApiResult<List<MenuVo>> list() {
        return ApiResult.success(this.menuService.list());
    }

    @PostMapping("/save")
    public ApiResult<String> save(@RequestBody Menu menu) {
        if (menu.getId() == null) {
            this.menuService.insert(menu);
            return ApiResult.success("添加成功");
        } else {
            this.menuService.update(menu);
            return ApiResult.success("更新成功");
        }
    }

    @DeleteMapping("/{id}")
    public ApiResult<String> remove(@PathVariable Long id) {
        this.menuService.delete(id);
        return ApiResult.success("删除成功");
    }

}
